﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GrainManage
{
    public class Convertor
    {
        public static DateTime? ToDateTime(string time)
        {
            DateTime dt;
            if (DateTime.TryParse(time, out dt))
            {
                return dt;
            }
            else
            {
                return null;
            }
        }

        public static int ToInt(string text)
        {
            int result = 0;
            int.TryParse(text.Trim(' '), out result);
            return result;
        }

        public static decimal ToDecimal(string text)
        {
            decimal result = 0m;
            decimal.TryParse(text.Trim(' '), out result);
            return result;
        }

        public static System.Windows.Media.ImageSource ToImage(byte[] data)
        {
            System.Windows.Media.Imaging.BitmapImage image = null;
            if (data != null && data.Length != 0)
            {
                image = new System.Windows.Media.Imaging.BitmapImage();
                image.BeginInit();
                image.StreamSource = new System.IO.MemoryStream(data);
                image.EndInit();
            }
            return image;
        }
    }
}
